package com.onemile.app.base;

import com.onemile.app.R;
import com.onemile.app.UIAccount;
import com.onemile.app.UICart;
import com.onemile.app.UIHome;
import com.onemile.app.UILogin;
import com.onemile.app.UIbaidu;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.utils.AsyncTaskManager;
import com.onemile.utils.ISupportAsyncTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public abstract class BaseUINoBottom extends Activity implements ISupportAsyncTask
{
   private AsyncTaskManager _taskManager;

   public AsyncTaskManager getTaskManager()
   {
	  if (_taskManager == null)
		 _taskManager = new AsyncTaskManager(this);
	  return _taskManager;
   }

   private LayoutInflater mInflater; // 实例化layout使用的类
   protected LinearLayout mainLayout; // 主题布局
   protected View contentView; // 页面中间UI部分

   protected View topToolbarView;// 顶部
   private TextView txtTitle;
   private ImageView imgBtnLeft;
   private ImageView imgBtnRight;
   private TextView txtCartNum;
   private RelativeLayout rightLay;

   private void InitBaseControls()
   {

	  // 实例化顶部工具栏布局，设置给完整页面Layout的顶部部分。。
	  this.mInflater = LayoutInflater.from(this);
	  topToolbarView = mInflater.inflate(R.layout.top_toolbar, null);
	  txtTitle = (TextView) topToolbarView.findViewById(R.id.txtTitle_toptoolbar);
	  imgBtnLeft = (ImageView) topToolbarView.findViewById(R.id.imgBtnLeft_toptoolbar);
	  imgBtnRight = (ImageView) topToolbarView.findViewById(R.id.imgBtnRight_toptoolbar);
	  txtCartNum = (TextView) topToolbarView.findViewById(R.id.txtCartNum_toptoolbar);
	  rightLay = (RelativeLayout) topToolbarView.findViewById(R.id.rayLayRight_toptoolbar);
	  imgBtnLeft.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			finish();
		 }
	  });
	  SetTopToolbar(txtTitle, imgBtnLeft, imgBtnRight, rightLay, txtCartNum);

	
	  topToolbarView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	  mainLayout.addView(topToolbarView);

	  contentView = mInflater.inflate(SetContentViewLayoutResId(), null);
	  contentView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1));
	  mainLayout.addView(contentView);
	
   }


   /**
    * 监听购物车，如果有更新则显示最新的数量
    */
//   private BroadcastReceiver receiver = new BroadcastReceiver()
//   {
//	  @Override
//	  public void onReceive(Context context, Intent intent)
//	  {
//		 try
//		 {
//			String action = intent.getAction();
//			if (action.equals(PubConst.Action.REFRESH_CART))
//			{
//			   int goodTypeNum = Session.getInstance().GetCommodityCount();
//			   if (goodTypeNum > 0)
//			   {
//				  txtCartNum.setVisibility(View.VISIBLE);
//				  txtCartNum.setText(String.valueOf(goodTypeNum));
//			   }
//			   else
//			   {
//				  txtCartNum.setVisibility(View.INVISIBLE);
//			   }
//			}
//		 }
//		 catch (Exception e)
//		 {
//			if (Session.getInstance().IsDEBUG())
//			{
//			   Log.e("ERROR", "BaseUI.receiver.new BroadcastReceiver() {...}_onReceive(context, intent) ", e);
//			}
//		 }
//
//	  }
//   };

//   protected void RefreshCartNum()
//   {
//	  try
//	  {
//		 int goodTypeNum = Session.getInstance().GetCommodityCount();
//		 if (goodTypeNum > 0)
//		 {
//			txtCartNum.setVisibility(View.VISIBLE);
//			txtCartNum.setText(String.valueOf(goodTypeNum));
//		 }
//		 else
//		 {
//			txtCartNum.setVisibility(View.INVISIBLE);
//		 }
//	  }
//	  catch (Exception e)
//	  {
//		 if (Session.getInstance().IsDEBUG())
//		 {
//			Log.e("ERROR", "BaseUI_RefreshCartNum() ", e);
//		 }
//	  }
//   }
   
   /**
    * 设置顶部工具条的风格(默认为1)
    * 
    * @param id
    *           1:green 2:blue 3:purple 4:orange 5:login
    */
   protected void SetTopSytle(int id)
   {
	  switch (id)
	  {
		 case 1:
			imgBtnLeft.setImageResource(R.drawable.top_btn_green);
			topToolbarView.setBackgroundResource(R.drawable.top_bg_green);
			break;
		 case 2:
			imgBtnLeft.setImageResource(R.drawable.top_btn_blue);
			topToolbarView.setBackgroundResource(R.drawable.top_bg_blue);
			break;
		 case 3:
			imgBtnLeft.setImageResource(R.drawable.top_btn_purple);
			topToolbarView.setBackgroundResource(R.drawable.top_bg_purple);
			break;
		 case 4:
			imgBtnLeft.setImageResource(R.drawable.top_btn_orange);
			topToolbarView.setBackgroundResource(R.drawable.top_bg_orange);
			break;
		 case 5:
			imgBtnLeft.setVisibility(View.INVISIBLE);
			imgBtnRight.setVisibility(View.INVISIBLE);
			txtCartNum.setVisibility(View.INVISIBLE);
			topToolbarView.setBackgroundResource(R.drawable.top_bg_login);
	  }
   }

   public void SetRightBtnCart()
   {
	  imgBtnRight.setOnClickListener(new View.OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	  	    Intent intent=new Intent();
	  	    intent.setClass(BaseUINoBottom.this, UICart.class);
	  	    startActivity(intent);
	     }
	  });
   }
   
   @Override
   final protected void onCreate(Bundle savedInstanceState)
   {
	  // 设置标题不显示
	  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	  super.onCreate(savedInstanceState);

	  // 创建出完整的页面Layout，并设置为当前Activity的页面。
	  mainLayout = new LinearLayout(this);
	  LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	  mainLayout.setOrientation(LinearLayout.VERTICAL);
	  mainLayout.setLayoutParams(layoutParams);
	  setContentView(mainLayout);

	  InitBaseControls();
	    
//	  registerReceiver(receiver, new IntentFilter(PubConst.Action.REFRESH_CART));

	  // 回调子类,正常处理onCreate方法。
	  onCreateOverride(savedInstanceState);
   }

   
   
   @Override
   protected void onResume()
   {
	  super.onResume();
//	  RefreshCartNum();
   }

   @Override
   protected void onDestroy()
   {
//	  unregisterReceiver(receiver);
	  super.onDestroy();
   }
   
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == event.KEYCODE_BACK)
	  {
		 finish();
	  }
	  return false;
   }

   
   /**
    * 子类实现后，在原来的onCreate方法中内容移到这里来操作。
    * 
    * @param savedInstanceState
    */
   protected abstract void onCreateOverride(Bundle savedInstanceState);

   /**
    * 返回layout xml的ID
    * 原本在Activity的onCreate方法中调用的setContentView(R.layout.xxxxLayoutId); 现在从该方法返回。
    * 该方法在onCreateOverride之前执行
    * 
    * @return
    */
   public abstract int SetContentViewLayoutResId();

   /**
    * 必须先设置顶部工具栏的参数, 该方法在onCreateOverride之前执行 默认右边rightRay，txtCartNum为不可见的
    */
   public abstract void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum);

   /**
    * 为避免子类错误调用，覆盖该方法，并定义为空方法。
    */
   public void setContentView(int layoutResID)
   {
   }

   /**
    * 执行异步任务
    */
   public String executeAsyncTask()
   {
	  return getTaskManager().executeAsyncTask();
   }

   /**
    * 执行异步任务
    * 
    * @param taskName
    */
   public String executeAsyncTask(String taskName)
   {
	  return getTaskManager().executeAsyncTask(taskName);
   }

   /**
    * 执行异步任务
    * 
    * @param taskName
    */
   public String executeAsyncTask(String taskName, Object[] params)
   {
	  return getTaskManager().executeAsyncTask(taskName, params);
   }

   /**
    * 取消异步任务
    */
   public void cancelAsyncTask()
   {
	  getTaskManager().cancelAsyncTask();
   }

   /**
    * 取消异步任务
    * 
    * @param taskId
    */
   public void cancelAsyncTask(String taskId)
   {
	  getTaskManager().cancelAsyncTask(taskId);
   }

   /**
    * 异步任务开始
    */
   public void onAsyncTaskStart(String taskName, Object[] params)
   {

   }

   /**
    * 异步任务执行
    * 
    * @param taskName
    */
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  return null;
   }

   /**
    * 异步任务完成
    * 
    * @param arg
    */
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {

   }

   /**
    * 异步任务被取消
    */
   public void onAsyncTaskCancelled(String taskName)
   {

   }

   /**
    * 异步任务发生错误
    * 
    * @param e
    */
   public void onAsyncTaskError(String taskId, String taskName, Exception e)
   {
	  // if (waitDialog != null)
	  // {
	  // waitDialog.dismiss();
	  // waitDialog = null;
	  // }
	  //
	  // if (e != null)
	  // {
	  // String msg = "";
	  // if (e instanceof SocketException)
	  // {
	  // msg = "手机网络不可用或发生网络读写错误";
	  // }
	  // else
	  // {
	  // msg = "手机网络不可用或发生网络读写错误";
	  // }
	  //
	  // if (msg != null && !msg.equals(""))
	  // {
	  // Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	  // }
	  // }
   }

}
